package com.qfedu.provider.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.config.RedisKeyConfig;
import com.qfedu.parm.CommentAdd;
import com.qfedu.provider.entity.DbComment;
import com.qfedu.provider.mapper.DbCommentMapper;
import com.qfedu.provider.mapper.DbUserMapper;
import com.qfedu.provider.service.DbCommentService;
import com.qfedu.utils.R;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Administrator
 * @description 针对表【db_comment(评论)】的数据库操作Service实现
 * @createDate 2024-11-23 11:16:39
 */
@Service
public class DbCommentServiceImpl extends ServiceImpl<DbCommentMapper, DbComment>
        implements DbCommentService {
    @Resource
    private DbCommentMapper dbCommentMapper;
    @Resource
    private DbUserMapper dbUserMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public R add(CommentAdd commentAdd, String token) {
        if (token == null) {
            return R.error("token不能为空,你还没有登录");
        }
        if (commentAdd == null) {
            return R.error("参数不能为空，你还没有写评论");
        }
        DbComment dbComment = new DbComment(commentAdd.getContent(), commentAdd.getProductId(), commentAdd.getProductTypeId(), commentAdd.getUserId());
        // BeanUtils.copyProperties(dbComment, commentAdd);
        if (dbCommentMapper.insert(dbComment) <= 0) {
            return R.error("评论失败");
        }
        return R.ok("评论成功");
    }

    @Override
    public R delete(Integer commentId, String token) {
        DbComment dbComment = dbCommentMapper.selectById(commentId);
        if (dbComment == null) {
            return R.error("评论不存在");
        }
        if (token == null) {
            return R.error("token不能为空,未登录");
        }
        System.out.println(token);
        // 从redis中获取token
        String token2 = stringRedisTemplate.opsForValue().get(RedisKeyConfig.AUTH_TOKEN+token);
        System.out.println("token2 = " + token2);
        if (token2 == null){
            return R.error("token失效，请重新登录");
        }

        dbComment.setDeleted(0);
        int i = dbCommentMapper.updateById(dbComment);
        if (i <= 0) {
            return R.error("删除失败");
        }
        return R.ok("删除成功");
    }
}




